package com.mohan.system.manage.realm;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.Serializable;

import static com.mohan.calligraphy.constant.Constant.REQUEST_HEADER;

@Slf4j
public class ShiroSessionManager extends DefaultWebSessionManager {

    public ShiroSessionManager() {
        super();
        this.setDeleteInvalidSessions(true);
    }

    @Override
    public Serializable getSessionId(ServletRequest request, ServletResponse response) {
        String token = WebUtils.toHttp(request).getHeader(REQUEST_HEADER);
        log.info(WebUtils.toHttp(request).getMethod());
        return  StringUtils.isNotBlank(token) ?
                token :
                super.getSessionId(request,response);
    }
}
